package com.whty.cms.filewps.config.controller;

import com.whty.cms.filewps.common.message.ResponseMessage;
import com.whty.cms.filewps.config.dto.ApplyInfo;
import com.whty.cms.filewps.config.dto.SourceInfo;
import com.whty.cms.filewps.config.service.IApplyInfoService;
import com.whty.cms.filewps.config.service.ISourceInfoService;
import com.whty.cms.filewps.util.MapUtil;
import com.whty.cms.filewps.util.MemcacheUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = {"/configure"})
public class ConfigureController {
	@Autowired
	private IApplyInfoService applyInfoService;
	
	@Autowired
	private ISourceInfoService sourceInfoService;
	
	
	@RequestMapping(value = "/getApplyInfo",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage getApplyInfo(){
		List<Map<String, String>> reslist = new ArrayList<Map<String, String>>();
		ResponseMessage result = new ResponseMessage();
        List<ApplyInfo> list  = applyInfoService.selectAll();
        for(ApplyInfo app : list) {
        	Map<String, String> map = new HashMap<>();
        	map.put("appId", app.getAppId());
        	map.put("appName", app.getAppName());
        	map.put("sourceBucket", app.getSourceBucket());
//        	map.put("destBucket", app.getDestBucket());
        	reslist.add(map);
        }
    	return returnMessage(result,"000000","成功",reslist);
    }
	
	@RequestMapping(value = "/getSourceInfo",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage getSourceInfo(){
		ResponseMessage result = new ResponseMessage();
        List<SourceInfo> list  = sourceInfoService.selectAll();
    	return returnMessage(result,"000000","成功",list);
    }
	
	@RequestMapping(value = "/saveApplyInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage saveApplyInfo(@RequestBody JSONObject json) {
		ResponseMessage result = new ResponseMessage();
		if(null==json||StringUtils.isBlank(json.optString("appName"))||StringUtils.isBlank(json.optString("sourceBucket"))
				||StringUtils.isBlank(json.optString("destBucket"))||StringUtils.isBlank(json.optString("public"))) {
			return returnMessage(result, "100000","必填参数为空","" );
		}
        try {
        	applyInfoService.saveApplyInfo(json);
            return ResponseMessage.getSuccessResponse();
        } catch (Exception ex) {
            return ResponseMessage.getFailedResponse();
        }
    }
	
	@RequestMapping(value = "/getAppSecret",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage getAppSecret(@RequestBody ApplyInfo applyInfo){
		ResponseMessage result = new ResponseMessage();
		if(null==applyInfo||StringUtils.isBlank(applyInfo.getAppName())) {
			return returnMessage(result, "100000","必填参数为空","" );
		}
		ApplyInfo appInfo  = applyInfoService.selectOne(applyInfo);
		Map<String, String> map = MapUtil.newHashMap();
		map.put("appId", appInfo.getAppId());
		map.put("appName", appInfo.getAppName());
		map.put("appSecret", appInfo.getPrivateKey());
    	return returnMessage(result,"000000","成功",map);
    }
	
	@RequestMapping(value = "/flushAll",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage flushAll(){
		ResponseMessage result = new ResponseMessage();
		MemcacheUtil.flushAll();
    	return returnMessage(result,"000000","成功",null);
    }
	
	private ResponseMessage returnMessage(ResponseMessage result, String code, String msg, Object data) {
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }
	
}
